## How do people evaluate foreign aid to “nasty” regimes?
## Tobias Heinrich & Yoshiharu Kobayashi
## British Journal of Political Science
#########################################################

## Script that executes the analysis

## Note: The prepping of the CCES data is omitted as the 
## CCES source file is large. The code is available as 
## references, but only prepped version will be copied.
## If anyone would like the original CCES file to replicate 
## that part as well, please email Tobias Heinrich
## (heinrict@mailbox.sc.edu) and we'll figure it out.


## Load all necessary packages, etc
###################################
set.seed(19811981)
setwd("/Users/th5/Dropbox/HeinrichKobayashi2017")
rm(list=ls())
library(foreign)
library(plyr)
library(grid)
library(matrixStats)
library(Matching)
library(gridExtra)
library(ebal)
library(reshape2)
library(tmvtnorm)
library(ggplot2)
source("x_Auxiliary Functions.R")


## Make folders
###############
dir.create(paste0(getwd(), "/output"), showWarnings=FALSE)
dir.create(paste0(getwd(), "/output/figures"), showWarnings=FALSE)
dir.create(paste0(getwd(), "/output/tables"), showWarnings=FALSE)
dir.create(paste0(getwd(), "/output/InArticle"), showWarnings=FALSE)


## 1) Set up the data
## If the original CCES file is available, you can change
## redo_CCES to TRUE and redo the tweaking of the data.
redo_CCES <- FALSE
source("1_Prep the data.R")

## 2) Show imbalance 
source("2_Run eBal.R")

## 3) Run non-experimental regressions
n_bs <- 500
source("3_NonExp Regressions.R")

## 4) Run experimental regressions
source("4_Exp Regressions.R")

## 5) Coefficient graphs
source("5_Make coefficient graph.R")

## 6) Effects graphs
source("6_Make effects graphs.R")

## 7) Check LR dependence
source("7_LR dependence.R")

## 8) Copy files to match names in article
file.copy(from="Screenshot-1.png",
          to="output/InArticle/Figure1.png", overwrite=TRUE)
file.copy(from="output/figures/ExpRegression-A2-Complex.pdf",
          to="output/InArticle/Figure2.pdf", overwrite=TRUE)
file.copy(from="output/figures/IC-PI-B-C-w1.pdf",
          to="output/InArticle/Figure3.pdf", overwrite=TRUE)
file.copy(from="output/figures/IC-Remedy-w1.pdf",
          to="output/InArticle/Figure4.pdf", overwrite=TRUE)


